package com.example.myapp;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

    private Fragment homeFragment;
    private Fragment contactsFragment;
    private Fragment profileFragment;

    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // 获取每个Tab的LinearLayout
        LinearLayout tabHome = findViewById(R.id.tab_home);
        LinearLayout tabContacts = findViewById(R.id.tab_contacts);
        LinearLayout tabProfile = findViewById(R.id.tab_profile);

        // 初始化Fragment
        homeFragment = new HomeFragment();
        contactsFragment = new ContactsFragment();
        profileFragment = new ProfileFragment();

        // 默认显示首页Fragment
        loadFragment(homeFragment);

        // 设置点击菜单
        tabHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 只有当前 Fragment 不同于目标 Fragment 时才切换
                if (currentFragment != homeFragment) {
                    hideAll();
                    show(homeFragment);
                }
            }
        });

        tabContacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentFragment != contactsFragment) {
                    hideAll();
                    show(contactsFragment);
                }
            }
        });

        tabProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentFragment != profileFragment) {
                    hideAll();
                    show(profileFragment);
                }
            }
        });
    }

    // 加载Fragment的方法
    private void loadFragment(Fragment fragment) {
        // 初始加载 Fragment
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();
        currentFragment = fragment;
    }

    // 隐藏所有Fragment
    private void hideAll() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(homeFragment);
        transaction.hide(contactsFragment);
        transaction.hide(profileFragment);
        transaction.commit();
    }

    // 显示指定的Fragment
    private void show(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment != null) {
            if (!fragment.isAdded()) {
                transaction.add(R.id.fragment_container, fragment); // 如果Fragment没有被添加，则添加
            }
            transaction.show(fragment);
            transaction.commit();
            currentFragment = fragment; // 更新当前显示的Fragment
        }
    }
}
